<?php

namespace JLPay\Models\Openmerch\Device;

use JLPay\Models\BaseResponse;

/**
 * 银联码查询响应
 */
class UnionPayCodeQueryResponse extends BaseResponse
{
    protected ?array $qrcodes = null;

    /**
     * 构造函数
     *
     * @param array|null $responseArray
     */
    public function __construct(?array $responseArray = null)
    {
        parent::__construct($responseArray);
        
        if ($responseArray) {
            $this->qrcodes = $responseArray['qrcodes'] ?? null;
        }
    }

    /**
     * 获取二维码列表
     *
     * @return array
     */
    public function getQrcodeList(): array
    {
        return $this->qrcodes ?? [];
    }

    /**
     * 获取第一个二维码信息
     *
     * @return array|null
     */
    public function getFirstQrcode(): ?array
    {
        if ($this->qrcodes && count($this->qrcodes) > 0) {
            return $this->qrcodes[0];
        }
        return null;
    }

    /**
     * 根据终端号获取二维码信息
     *
     * @param string $termNo
     * @return array|null
     */
    public function getQrcodeByTermNo(string $termNo): ?array
    {
        if ($this->qrcodes) {
            foreach ($this->qrcodes as $qrcode) {
                if (($qrcode['term_no'] ?? '') === $termNo) {
                    return $qrcode;
                }
            }
        }
        return null;
    }

    /**
     * 转换为数组
     *
     * @return array
     */
    public function toArray(): array
    {
        $data = parent::toArray();
        $data['qrcodes'] = $this->qrcodes;
        return $data;
    }
} 